<?php

// require_once('mdetect.class.php');


class template{

  	var $url_header="";
  	var $url_footer="";
	var $url_template="";
	var $output = "";
	var $smartphone = false;
	var $arr_tpl_mobile = array();	

	function template() 
		{

                            
		}

	/**
	 * String replace an associative array of comments
	 *
	 * @param array $array
	 */
	function sostituisci_array($array){
			
		foreach($array as $commento => $stringa){
			
			$this->sostituisci($commento,$stringa);
			
			}
		}
	
	/**
	 * String replace a file
	 *
	 * @param string $commento
	 * @param string $file
	 */
	function sostituisci_file($commento,$file){
		
		if($this->smartphone){
			$file = str_replace(TEMPLATE_DIR,$this->arr_tpl_mobile['TEMPLATE_DIR']['new'],$file);
		}
		
		$this->sostituisci($commento,file_get_contents($file));
	}
	
	/**
	 * String replace of comments
	 *
	 * @param string $commento
	 * @param string $stringa
	 */
	function sostituisci($commento="",$stringa=""){
				
			if($this->smartphone){
				foreach($this->arr_tpl_mobile as $tpl){
					$stringa = str_replace($tpl['original'],$tpl['new'],$stringa);
				}
			}
		
			if(is_string($stringa))$this->output = str_replace("<!-- $commento !-->", $stringa, $this->output);
			if(is_array($stringa))$this->output = str_replace("<!-- $commento !-->", implode($stringa), $this->output);
		}

	/**
	 * Stampa l'output della pagina
	 *
	 */
	function output() {	

      			if(!DEBUG){
      				ob_start('ob_gzhandler') ;
      				header("Content-Encoding: gzip");
      				header("Content-type: text/html; charset=utf-8");
      			}
      			echo $this->output;

		}	
	/**
	 * Legge un template da un dato url
	 *
	 */
	function read_template()
	{
	
		
		
		
		$this->output = "";
    	
		if($this->url_header){ $this->output .= file_get_contents($this->url_header); }					 
		$this->output .= file_get_contents($this->url_template);
		if($this->url_footer){ $this->output .= file_get_contents($this->url_footer); }
		
     	if(defined('TEMPLATE_DIR'))$this->sostituisci("TEMPLATE_DIR",TEMPLATE_DIR);
     	if(defined('TEMPLATE_IMAGES_DIR'))$this->sostituisci("TEMPLATE_IMAGES_DIR",TEMPLATE_IMAGES_DIR);


    }

}

class pagina extends template{

	var $script_name;
	var $title;	
	var $meta;
	
	/**
	 * Costruttore, inizializza i template e lo script name
	 *
	 * @return pagina
	 */
	function __construct(){
		$this->url_header=TEMPLATE_DIR."header.htm";
		$this->url_footer=TEMPLATE_DIR."footer.htm";
  		$this->script_name = substr($_SERVER['SCRIPT_NAME'],strrpos($_SERVER['SCRIPT_NAME'],'/')+1);
    }
	/**
	 * Legge il template ed effettua tutte le sostituzioni di base
	 *
	 */
	function read_template()
		{

			/**
			 * Verifica il tipo di apparato e modifica il TEMPLATE_DIR
			 */
			$device = new uagent_info();
			
			if($device->DetectSmartphone() == 1 || $device->DetectTierTablet() == 1){
				$this->smartphone = true;
			}
							
			if($this->smartphone){
				
				if(defined('TEMPLATE_DIR')){
					$this->arr_tpl_mobile['TEMPLATE_DIR']['original'] = TEMPLATE_DIR;
					$this->arr_tpl_mobile['TEMPLATE_DIR']['new'] = str_replace(array('/site/','/my/', '/ws/', '/facebook/'), array('/site_mobile/','/my_mobile/', '/ws_mobile/', '/facebook_mobile/'), TEMPLATE_DIR);
				}
				if(defined('TEMPLATE_IMAGES_URL')){
					$this->arr_tpl_mobile['TEMPLATE_IMAGES_URL']['original'] = TEMPLATE_IMAGES_URL;
					$this->arr_tpl_mobile['TEMPLATE_IMAGES_URL']['new'] = str_replace(array('/images/site/','/images/my/', '/images/ws/', '/images/facebook/'), array('/images/site_mobile/','/images/my_mobile/', '/images/ws_mobile/', '/images/facebook_mobile/'), TEMPLATE_IMAGES_URL);
				}
				if(defined('CSS_URL')){
					$this->arr_tpl_mobile['CSS_URL']['original'] = CSS_URL;
					$this->arr_tpl_mobile['CSS_URL']['new'] = str_replace(array('/site/','/my/', '/ws/', '/facebook/'), array('/site_mobile/','/my_mobile/', '/ws_mobile/', '/facebook_mobile/'), CSS_URL);
				}
				if(defined('JS_URL')){
					$this->arr_tpl_mobile['JS_URL']['original'] = JS_URL;
					$this->arr_tpl_mobile['JS_URL']['new'] = str_replace(array('/site/','/my/', '/ws/', '/facebook/'), array('/site_mobile/','/my_mobile/', '/ws_mobile/', '/facebook_mobile/'), JS_URL);
				}
				if(defined('SWF_URL')){
					$this->arr_tpl_mobile['SWF_URL']['original'] = SWF_URL;
					$this->arr_tpl_mobile['SWF_URL']['new'] = str_replace(array('/site/','/my/', '/ws/', '/facebook/'), array('/site_mobile/','/my_mobile/', '/ws_mobile/', '/facebook_mobile/'), SWF_URL);
				}
							
				$this->url_header = str_replace(TEMPLATE_DIR,$this->arr_tpl_mobile['TEMPLATE_DIR']['new'],$this->url_header);
				$this->url_footer = str_replace(TEMPLATE_DIR,$this->arr_tpl_mobile['TEMPLATE_DIR']['new'],$this->url_footer);
				$this->url_template = str_replace(TEMPLATE_DIR,$this->arr_tpl_mobile['TEMPLATE_DIR']['new'],$this->url_template);
			}
			
			$this->output = "";
		    	
				if($this->url_header){
						
						$this->output.= file_get_contents($this->url_header);

						if(!empty($this->js)){
							if(is_array($this->js)){
									foreach($this->js as $js){
											if(strpos($js,'http:') === false && strpos($js,'https:') === false){
												if(strpos($js,'.min.') === false && isset($this->arr_tpl_mobile['JS_URL'])){
													if($this->smartphone) $JS_DIR = str_replace(JS_DIR,$this->arr_tpl_mobile['JS_URL']['new'],JS_DIR);
													else $JS_DIR = JS_DIR;
													if(file_exists($JS_DIR.substr($js,0,-3).'.min.js')) $js = substr($js,0,-3).'.min.js';
												}
												$this->sostituisci("CCPHP_JS","\t".'<script type="text/javascript" language="javascript" src="<!-- JS_DIR !-->'.$js.'"></script>'."\n".'<!-- CCPHP_JS !-->');
											}else{
												$this->sostituisci("CCPHP_JS","\t".'<script type="text/javascript" language="javascript" src="'.$js.'"></script>'."\n".'<!-- CCPHP_JS !-->'); 
											}
										}
							}else{
								if(strpos($this->js,'http:') === false && strpos($this->js,'https:') === false){
									if(strpos($this->js,'.min.') === false){
										if($this->smartphone) $JS_DIR = str_replace(JS_DIR,$this->arr_tpl_mobile['JS_URL']['new'],JS_DIR);
										else $JS_DIR = JS_DIR;
										if(file_exists($JS_DIR.substr($this->js,0,-3).'.min.js')) $this->js = substr($this->js,0,-3).'.min.js';
									}
									$this->sostituisci("CCPHP_JS","\t".'<script type="text/javascript" language="javascript" src="<!-- JS_DIR !-->'.$this->js.'"></script>'."\n".'<!-- CCPHP_JS !-->');
								}else{
									$this->sostituisci("CCPHP_JS","\t".'<script type="text/javascript" language="javascript" src="'.$this->js.'"></script>'."\n".'<!-- CCPHP_JS !-->'); 
								}
							}
						}
						if(!empty($this->js_script)){
							foreach($this->js_script as $js_script){
								if(strpos($js_script['src'],'http:') === false && strpos($js_script['src'],'https:') === false) $this->sostituisci("CCPHP_JS","\t".'<script type="text/javascript" language="javascript" src="<!-- JS_DIR !-->'.$js_script['src'].'">'.$js_script['script'].'</script>'."\n".'<!-- CCPHP_JS !-->');
								else $this->sostituisci("CCPHP_JS","\t".'<script type="text/javascript" language="javascript" src="'.$js_script['src'].'">'.$js_script['script'].'</script>'."\n".'<!-- CCPHP_JS !-->'); 
							}
						}
						if(!empty($this->js_file)){
							if(is_array($this->js_file)){
									foreach($this->js_file as $js_file){
										if($this->smartphone) $js_file = str_replace(TEMPLATE_DIR,$this->arr_tpl_mobile['TEMPLATE_DIR']['new'],$js_file);
										$this->sostituisci("CCPHP_JS","\t".'<script type="text/javascript" language="javascript">'."\n".file_get_contents($js_file)."\n\t".'</script>'."<!-- CCPHP_JS !-->");
									}
							}else{
								if($this->smartphone) $this->js_file = str_replace(TEMPLATE_DIR,$this->arr_tpl_mobile['TEMPLATE_DIR']['new'],$this->js_file);
								$this->sostituisci("CCPHP_JS","\t".'<script type="text/javascript" language="javascript">'."\n".file_get_contents($this->js_file)."\n\t".'</script>'."<!-- CCPHP_JS !-->");
							}
						}

						//google analytics
						if(defined('GOOGLE_ANALYTICS')){ $this->sostituisci("CCPHP_JS","\t".'<script type="text/javascript" language="javascript">'."\n".file_get_contents(JS_DIR.GOOGLE_ANALYTICS)."\n\t".'</script>'."<!-- CCPHP_JS !-->"); }
						
						$this->sostituisci("CCPHP_JS",'');
						
						if(!empty($this->css)){
							if(is_array($this->css)){
									foreach($this->css as $css){
										if(strpos($css,'.min.') === false){
											if($this->smartphone && isset($this->arr_tpl_mobile['CSS_URL'])) $CSS_DIR = str_replace(CSS_DIR,$this->arr_tpl_mobile['CSS_URL']['new'],CSS_DIR);
											else $CSS_DIR = CSS_DIR;
											if(file_exists($CSS_DIR.substr($css,0,-4).'.min.css')) $css = substr($css,0,-4).'.min.css';
										}
										$this->sostituisci("CCPHP_CSS","\t@import '<!-- CSS_DIR !-->".$css."';\n<!-- CCPHP_CSS !-->");
									}
							}
							else{
								if(strpos($this->css,'.min.') === false && isset($this->arr_tpl_mobile['CSS_URL'])){
									if($this->smartphone) $CSS_DIR = str_replace(CSS_DIR,$this->arr_tpl_mobile['CSS_URL']['new'],CSS_DIR);
									else $CSS_DIR = CSS_DIR;
									if(file_exists($CSS_DIR.substr($this->css,0,-4).'.min.css')) $this->css = substr($this->css,0,-4).'.min.css';
								}
								$this->sostituisci("CCPHP_CSS","\t@import '<!-- CSS_DIR !-->".$this->css."';\n<!-- CCPHP_CSS !-->");
							}
						}
						$this->sostituisci("CCPHP_CSS",'');		
				}
									 
				if($this->url_template){ $this->output .= file_get_contents($this->url_template); }
				if($this->url_footer){ $this->output .= file_get_contents($this->url_footer); }				 
							
				if(isset($this->title)){$this->sostituisci("TITLE",$this->title);}
				else if(defined('CCPHP_TITLE')){$this->sostituisci("TITLE",CCPHP_TITLE); }
				
				if(defined('CCPHP_TITLE'))$this->sostituisci("CCPHP_TITLE",CCPHP_TITLE);
		     	if(defined('TEMPLATE_DIR'))$this->sostituisci("TEMPLATE_DIR",TEMPLATE_DIR);
		     	
		     	if(defined('CSS_URL')){ $this->sostituisci("CSS_DIR",CSS_URL); }
		     	else if(defined('CSS_DIR')){ $this->sostituisci("CSS_DIR",CSS_DIR); }
		     	
				if(defined('JS_URL')){ $this->sostituisci("JS_DIR",JS_URL); }
		     	else if(defined('JS_DIR')){ $this->sostituisci("JS_DIR",JS_DIR); }

		     	if(defined('SWF_URL')){ $this->sostituisci("SWF_DIR",SWF_URL); }
		     	else if(defined('SWF_DIR')){ $this->sostituisci("SWF_DIR",SWF_DIR); }
		     	
				if(defined('TEMPLATE_IMAGES_URL')){$this->sostituisci("TEMPLATE_IMAGES_DIR",TEMPLATE_IMAGES_URL);}
     			else if(defined('TEMPLATE_IMAGES_DIR')){$this->sostituisci("TEMPLATE_IMAGES_DIR",TEMPLATE_IMAGES_DIR);}
     			
     			if(defined('URL_DIR')){ $this->sostituisci("URL_DIR",URL_DIR); }
     			
				$this->read_meta();
		}
		
	/**
	 * Legge il template ed effettua le sostituzioni dei meta
	 */
		
	function read_meta(){
		
     			if(empty($this->meta['robots'])){ $this->sostituisci("META_ROBOTS","noindex,nofollow");}
     			else{ $this->sostituisci("META_ROBOTS",$this->meta['robots']);}
     			
	     		if(empty($this->meta['description'])){ $this->sostituisci("META_DESCRIPTION","");}
     			else{ $this->sostituisci("META_DESCRIPTION",$this->meta['description']);}
     			
	     		if(empty($this->meta['keywords'])){ $this->sostituisci("META_KEYWORDS","");}
     			else{ $this->sostituisci("META_KEYWORDS",$this->meta['keywords']);}
     			
     			if(empty($this->meta['gplus'])){ $this->sostituisci("META_GPLUS","");}
     			else{ $this->sostituisci("META_GPLUS",$this->meta['gplus']);}
				
	}
	
}
	

class pagina_utente_profilo extends pagina{
	
	/**
	 * Legge il template ed effettua tutte le sostituzioni di base
	 *
	 */
	function get_dropdown(){
		$gg='';
		for($i=1;$i<=31;$i++){
			$n = str_pad($i,2,"0",STR_PAD_LEFT);
			$gg.= '<option value="'.$n.'">'.$n.'</option>';
		}
		$this->sostituisci('SELECT_GIORNO',$gg);

		$mm='';
		for($i=1;$i<=12;$i++){
			$n = str_pad($i,2,"0",STR_PAD_LEFT);
			$mm.= '<option value="'.$n.'">'.$n.'</option>';
		}
		$this->sostituisci('SELECT_MESE',$mm);

		$aa='';
		for($i=date("Y")-18;$i>=date("Y")-100;$i--){
			$aa.= '<option value="'.$i.'">'.$i.'</option>';
		}
		$this->sostituisci('SELECT_ANNO',$aa);
		
	}
}

class pagina_offerte extends pagina{

	/**
	 * Legge il template ed effettua tutte le sostituzioni di base
	 *
	 */
	function get_dropdown(){
		$gg='';
		for($i=1;$i<=31;$i++){
			$n = str_pad($i,2,"0",STR_PAD_LEFT);
			$gg.= '<option value="'.$n.'">'.$n.'</option>';
		}
		$this->sostituisci('SELECT_GIORNO',$gg);

		$mm='';
		for($i=1;$i<=12;$i++){
			$n = str_pad($i,2,"0",STR_PAD_LEFT);
			$mm.= '<option value="'.$n.'">'.$n.'</option>';
		}
		$this->sostituisci('SELECT_MESE',$mm);

		$aa='';
		for($i=date("Y");$i<=date("Y")+10;$i++){
			$aa.= '<option value="'.$i.'">'.$i.'</option>';
		}
		$this->sostituisci('SELECT_ANNO',$aa);
	}
	
	function get_dropdown_orario($minuti){
		$orario = '';
		for($i=0;$i<=1440;$i+=$minuti){
			$o = sprintf("%02d:%02d", floor($i/60), $i%60);
			$orario.= '<option value="'.$o.':00">'.$o.'</option>';
		}
		$this->sostituisci('SELECT_ORARIO',$orario);
	}
}

class pagina_scheda extends pagina{

	/**
	 * Legge il template ed effettua tutte le sostituzioni di base
	 *
	 */
	function get_dropdown_orario($minuti){
		$orario = '';
		for($i=0;$i<=1440;$i+=$minuti){
			$o = sprintf("%02d:%02d", floor($i/60), $i%60);
			$orario.= '<option value="'.$o.':00">'.$o.'</option>';
		}
		$this->sostituisci('SELECT_ORARIO',$orario);
	}
}


class pagina_ajax extends pagina{
	
	var $output_var = "";
	
	function sostituisci_in_var($commento="",$stringa=""){
		
		if($this->smartphone){
			foreach($this->arr_tpl_mobile as $tpl){
				$stringa = str_replace($tpl['original'],$tpl['new'],$stringa);
			}
		}
		
		if(is_string($stringa))$this->output_var = str_replace("<!-- $commento !-->", $stringa, $this->output_var);
		if(is_array($stringa))$this->output_var = str_replace("<!-- $commento !-->", implode($stringa), $this->output_var);
	}
	
	function sostituisci_in_var_file($commento,$file){
		
		if($this->smartphone){
			$file = str_replace(TEMPLATE_DIR,$this->arr_tpl_mobile['TEMPLATE_DIR']['new'],$file);
		}
		
		$this->sostituisci_in_var($commento,file_get_contents($file));
	}
	
}

class pagina_ws extends pagina{

	function output() {

		if(!DEBUG){
			ob_start('ob_gzhandler') ;
			header("Content-Encoding: gzip");
			header("Content-type: text/html; charset=utf-8");
		}
		echo $this->output;

	}

}

?>
